package java6681.user;

public class Register6681 {
    private static User6681[] userList;
    private static String errMessage;

    static {
        userList = new User6681[100];
        userList[0] = new User6681("zhang", "asdQWE123", "18356666666");
        userList[1] = new User6681("shu", "QWEasd666", "13166668888");
        userList[3] = new User6681("shree", "three123", "17166881123");
    }

    public Register6681() {

    }

    public static String getUserList() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < userList.length; i++) {
            if (userList[i] != null) {
                builder.append(userList[i].getName()).append("  ");
            }
        }
        return builder.toString();
    }
//    public User6681[] getUserList() {
//        userList = new User6681[4];
//        userList[0] = new User6681("zhang", "asdQWE123", "18356666666");
//        userList[1] = new User6681("shu", "QWEasd666", "13166668888");
//        return userList;
//    }

    public static String getErrMessage() {
        return errMessage;
    }

    public static boolean checkName6681(String name) {
        for (int i = 0; i < userList.length; i++) {
            if (userList[i] != null && userList[i].getName().equals(name)) {
                errMessage = "用户名已存在，请重新输入！";
                return false;
            }
        }
        return true;
        /*if (name == null) {
            errMessage = "用户名不能为空！";
            return false;
        }
            if (name.equalsIgnoreCase(String.valueOf(userList))) {
                if (!(name.equals(userList))) {
                    errMessage = "用户名已存在，请重新输入！";
                    return false;
                }
            }*/
    }

    public static boolean checkPassword6681(String pwd, String pw) {
        int len = pwd.length();
        int nUpper = 0, nLower = 0, nDig = 0;
        if (len < 6) {
            errMessage = "密码的长度不能低于6位";
            return false;
        }
        for (int i = 0; i < len; i++) {
            char c = pwd.charAt(i);
            if (c >= 'A' && c <= 'Z') {
                nUpper++;
            } else if (c >= 'a' && c <= 'z') {
                nLower++;
            } else if (c >= '0' && c <= '9') {
                nDig++;
            }
        }
        if (nUpper < 1 || nLower < 1 || nDig < 1) {
            errMessage = "密码至少包含1个大写字母、1个小写字母、1个数字";
            return false;
        }
        if (!(pwd.equals(pw))) {
            errMessage = "两次输入的密码要相同！";
            return false;
        }
        return true;
    }

    public static boolean checkPhone6681(String telNumber) {
        int len = telNumber.length();
        if (len != 11) {
            errMessage = "手机号码必须是11位数字，请重新输入！";
            return false;
        } else if (!(telNumber.startsWith("13") || telNumber.startsWith("15")
                || telNumber.startsWith("17") || telNumber.startsWith("18"))) {
            errMessage = "手机号码必须以13、15、17、18开头，请重新输入!";
            return false;
        }
        for (int i = 2; i < len; i++) {
            char c = telNumber.charAt(i);
            if (!Character.isDigit(c)) {
                errMessage = "手机号码必须是数字，请重新输入";
                return false;
            }
        }
        return true;
    }


    public static boolean addUser6681(String name, String pwd, String pw, String telNumber) {
      if (checkName6681(name)&&checkPassword6681(pwd, pw)&&checkPhone6681(telNumber)){
          User6681 user = new User6681(name,pwd,telNumber);
          for (int i = 0; i < userList.length; i++) {
              if (userList[i] == null){
                  userList[i]=user;
                  return true;
              }
          }
      }else{
          return false;
      }
      errMessage = "空间不足";
      return false;
        /*if (!(checkName6681(name) && checkPassword6681(pwd, pw) && checkPhone6681(telNumber))) {
            errMessage = "错误";
        }*/
    }

}
